package lib_model.Cart;

import java.util.ArrayList;

/**
 * @author SHINGUYENXUAN
 * 
 */

public class Cart {
	private ArrayList<ItemCart> list;

	/**
	 * @return
	 */
	public ArrayList<ItemCart> getListCart() {
		if (list == null)
			list = new ArrayList<ItemCart>();
		return this.list;
	}

	/**
	 * @param item
	 */

	public void AddItemCart(ItemCart item) {
		if (this.list == null) {
			list = new ArrayList<ItemCart>();
			this.list.add(item);
		} else {
			System.out.println(this.list.size());
			boolean f = true;
			for (int i = 0; i < this.list.size(); i++) {
				if (list.get(i).CheckID(item.id)) {
					list.get(i).ChangeQuantity(list.get(i).quantity + 1);
					f = false;
					break;
				}
			}
			if (f) {
				System.out.println("Add Them");
				this.list.add(item);
			}
		}
	}

	/**
	 * @param id
	 * @param quantity
	 */
	public void ChangeQuantity(int id, int quantity) {
		for (int i = 0; i < this.list.size(); i++) {
			if (list.get(i).CheckID(id)) {
				list.get(i).ChangeQuantity(quantity);
			}
		}
	}

	/**
	 * @param id
	 */
	public void RemoveItemCart(int id) {
		for (int i = 0; i < this.list.size(); i++) {
			if (list.get(i).CheckID(id)) {
				this.list.remove(list.get(i));
				break;
			}
		}
	}
	
	/**
	 * @return
	 */
	public float TotalCart() {
		float t = 0;
		for (int i = 0; i < this.list.size(); i++) {
			t += list.get(i).getTotalPrice();
		}
		return t;
	}

	public boolean CheckID(int ID) {
		for (int i = 0; i < this.list.size(); i++) {
			if (list.get(i).CheckID(ID)) {
				return true;
			}
		}
		return false;
	}
	
	public int getQuantity(int ID){
		for (int i = 0; i < this.list.size(); i++) {
			if (list.get(i).CheckID(ID)) {
				return list.get(i).quantity;
			}
		}
		return 0;
	}
	
	public int getTotalQuantity(){
		int i = 0;
		for (ItemCart item : this.list) {
			i = i + item.quantity;
		}
		return i;
		
	}
}
